/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.cmu.lti.sepia.infra.run;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

import edu.cmu.lti.sepia.data.xml.IXmlHandler;
import edu.cmu.lti.sepia.infra.run.model.Document;
import edu.cmu.lti.sepia.infra.run.model.RetrievalResult;
import edu.cmu.lti.sepia.infra.run.model.RetrievalResultSet;

public class RetrievalResultXmlToObj extends DefaultHandler implements IXmlHandler {
	
	enum XML {
		TOPIC, ID, DOCUMENT, DOCID, SCORE, METADATA, RUNID, DESCRIPTION
	}
	
	private RetrievalResultSet result = new RetrievalResultSet();

  private boolean inRunid = false;
  private boolean inDescription = false;
  
	private String currentId;
	private RetrievalResult currentResult;
	
	private StringBuilder buffer = new StringBuilder();
	
	@Override
	public void startElement( String uri, String localName, 
			String qName, Attributes attributes ) {
	  if ( XML.RUNID.toString().equals(qName) ) {
      inRunid = true;
      buffer = new StringBuilder();
    } else if ( XML.DESCRIPTION.toString().equals(qName) ) {
      inDescription = true;
      buffer = new StringBuilder();
    } else if ( XML.TOPIC.toString().equals(qName) ) {
			currentResult = new RetrievalResult();
			currentId = attributes.getValue(XML.ID.toString());
		} else if ( XML.DOCUMENT.toString().equals(qName) ) {
			String docidText = attributes.getValue(XML.DOCID.toString());
			double score;
			String scoreText = attributes.getValue(XML.SCORE.toString());
      if (scoreText!=null) {
        score = Double.parseDouble( scoreText );
      } else {
        score = 1.0D;
      }
			Document d = new Document( docidText, score );
			currentResult.add(d);
		}
	}
	
	@Override
  public void characters(char[] ch, int offset, int length) {
    if (inRunid||inDescription) {
      String value = new String(ch, offset, length);
      buffer.append(value);
    }
  }
	
	@Override
	public void endElement( String uri, String localName, String qName ) {
    if (inRunid) {
      result.getMetadata().put("RUNID", buffer.toString());
      inRunid = false;
    } else if (inDescription) {
      result.getMetadata().put("DESCRIPTION", buffer.toString());
      inDescription = false;
    }

    //end of file	  
	  if ( XML.TOPIC.toString().equals(qName) ) {
			result.put(currentId, currentResult);
		}
	}
	
	public RetrievalResultSet getParsed() {
	  return result;
	}
	
}
